<?php

/*

Smart Bot (smart_bot.php)

*/

$page_title = "Smart Bot";
$rank_check = 0;
include "header.inc.php";

$findQ = mysql_query("SELECT * FROM smart_bot WHERE game = '' OR game = '$game'");
$numQ = mysql_num_rows($findQ);

if ($page == "add2")
{
	if ($rank < 7) { die("$openHTML<p>You must be an admin to view this page.$closeHTML"); }

	if (($game == "1") AND ($rank == "7") AND ($username == "steve") AND ($all_games == 1))
	{
		$game2 = "";
	}
	else
	{
		$game2 = "$game";
	}

	mysql_query("INSERT INTO smart_bot (question,answer,game) VALUES ('$question','$answer','$game2')");
	header("Location: smart_bot.php?page=add&game=$game");
}

print "$openHTML";

if (!$page)
{

ECHO <<<END

<form action=smart_bot.php?page=search&game=$game method=post>
<p align=center>
Hey Smart Bot, I was just wondering:<Br>
<input type=text name=question size=60 value="Type your question here"><br>
<input type=submit value="Ask Away">
</p>
</form>

END;

if ($rank == 7) { print "<p><a href=smart_bot.php?game=$game&page=add>Add an entry!</a></p>"; }

}

if ($page == "search")
{
		while ($getQ = mysql_fetch_array($findQ))
		{
			$id = $getQ[id];
			$simuPTS[$id] = similar_text($question, $getQ[question], $simPTS[$id]);
			if ($simPTS[$id] >= 65) {
					$simPTS[$id] = number_format($simPTS[$id], 2, '.', '');
					if (!$f) { print "<p>$username, I did a litte search through my huge brain and found these results:</p>"; }

						print "<a href=smart_bot.php?page=view&id=$getQ[id]&game=$game>$getQ[question]</a> (Relevancy: $simPTS[$id]%)<Br>";
						$f = 1;
			}
		}

	if (!$f) { print "<p><i>Sorry $username, I couldn't find ANY questions with your search terms. Try again with some different words.</i></p><p>Search Tips:<br><br>* Be sure to use full words, no abbreviations. <br>* Make sure all words are spelled correctly.<Br>* Try different keywords.<br>* Try more general keywords."; }

print "<p>Searching $numQ entries in my database!</p>";

}

if ($page == "view")
{
	$findQ4 = fetch("SELECT * FROM smart_bot WHERE id = '$id'");

	$findPage[page_info] = "$findQ4[answer]";
	include "showpage.inc.php";

	print "

	<p><b>$findQ4[question]</b></p>

	<p>$findPage[page_info]</p>

	<p>Related Questions:</p>

	";

	$findQs = mysql_query("SELECT * FROM smart_bot WHERE game = '' OR game = '$game'");
	while ($getQs = mysql_fetch_array($findQs))
	{
			$simuPTS[$id] = similar_text($findQ4[question], $getQs[question], $simPTS[$id]);
			if ($simPTS[$id] >= 65)
			{
				print "<font size=-1><a href=smart_bot.php?page=view&id=$getQs[id]&game=$game>$getQs[question]</a></font><Br>";
		$d++;
			}
	}

	if ($d <= 0) { print "<i>No related questions</i>"; }

	print "</p>";

	print "<p>Searching $numQ entries in my database!</p>";

}

if ($page == "add")
{
if ($rank < 7) { die("You must be an admin to view this page.$closeHTML"); }
ECHO <<<END

<p>
<form action=smart_bot.php?page=add2&game=$game method=post>
Question:<br>
<input type=text name=question size=60><br><br>

Answer:<br>
<textarea name=answer cols=52 rows=10 name=answer></textarea><br><br>

END;

if (($game == "1") AND ($rank == "7") AND ($username == "steve"))
{
	print "<input type=checkbox name=all_games value=1> Check if question is for ALL games<br><br>";
}

print "<input type=submit value=\"Add Entry\">
</form>
</p>
";

}

print "$closeHTML";

?>